﻿@page "/profile/create"
@attribute [Authorize(Roles = "Administrator")]
@inherits OwningComponentBase<ICovenantService>

@using Microsoft.JSInterop
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Components.Authorization
@using Covenant.Core
@using Covenant.Models.Covenant
@using Covenant.Models.Listeners
@inject UserManager<CovenantUser> UserManager
@inject IJSRuntime IJSRuntime
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager NavigationManager

<div class="d-flex justify-content-between flex-wrap flex-md-nowrap pb-2 mb-3">
    <h1 class="h2">Create Profile</h1>
</div>

<ul class="nav nav-tabs mb-3" id="listener-tab" role="tablist">
    <li class="nav-item">
        <a class="nav-link" id="http-tab" data-toggle="tab" href="#http" role="tab" aria-controls="http" aria-selected="false">
            <span class="fe fe-cloud"></span>
            HttpProfile
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="bridge-tab" data-toggle="tab" href="#bridge" role="tab" aria-controls="bridge" aria-selected="false">
            <span class="fe fe-radio"></span>
            BridgeProfile
        </a>
    </li>
</ul>

<div class="tab-content" id="listener-tab-content">
    <div class="tab-pane fade" id="http" role="tabpanel" aria-labelledby="http-tab">
        <HttpProfileForm Profile="@(new HttpProfile())" OnSubmit="OnCreateHttpProfile" SubmitIcon="plus" SubmitLabel="Create"></HttpProfileForm>
    </div>
    <div class="tab-pane fade" id="bridge" role="tabpanel" aria-labelledby="bridge-tab">
        <BridgeProfileForm Profile="@(new BridgeProfile())" OnSubmit="OnCreateBridgeProfile" SubmitIcon="plus" SubmitLabel="Create"></BridgeProfileForm>
    </div>
</div>

@code {
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await IJSRuntime.InvokeAsync<object>("ShowTab", new[] { "#http-tab" });
    }

    private async Task OnCreateHttpProfile(HttpProfile profile)
    {
        try
        {
            AuthenticationState state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
            await Service.CreateHttpProfile(profile, await UserManager.GetUserAsync(state.User));
            NavigationManager.NavigateTo($"/listener");
        }
        catch (Exception e) when (e is ControllerNotFoundException || e is ControllerBadRequestException || e is ControllerUnauthorizedException)
        {
            // ModelState.AddModelError(string.Empty, e.Message);
            // return RedirectToAction(nameof(Create), new { id = launcher.Name });
        }
    }

    private async Task OnCreateBridgeProfile(BridgeProfile profile)
    {
        try
        {
            AuthenticationState state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
            await Service.CreateBridgeProfile(profile, await UserManager.GetUserAsync(state.User));
            NavigationManager.NavigateTo($"/listener");
        }
        catch (Exception e) when (e is ControllerNotFoundException || e is ControllerBadRequestException || e is ControllerUnauthorizedException)
        {
            // ModelState.AddModelError(string.Empty, e.Message);
            // return RedirectToAction(nameof(Create), new { id = launcher.Name });
        }
    }
}